//
//  LoginViewController.m
//  e-nurse
//
//  Created by  on 9/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "LoginViewController.h"
#import "RootViewController.h"
#import "ServerProxy.h"
#import "User.h"
#import <QuartzCore/QuartzCore.h>
#import "RegisterViewController.h"
#import "ForgetPasswordResult.h"
#import "ActivateUserViewController.h"
#import "LoginResult.h"
#import "ALToastView.h"

@interface LoginViewController ()

- (void)loginCompleted:(LoginResult*)loginResult;
- (void)loginCompletedImpl:(LoginResult*)loginResult;

- (void)forgetPasswordCompleted:(ForgetPasswordResult*)forgetPassResult;
- (void)forgetPasswordCompletedImpl:(ForgetPasswordResult*)forgetPassResult;

- (void)close;

@end

@implementation LoginViewController
@synthesize activityIndicatorView = _activityIndicatorView;
@synthesize loginNavigationItem = _loginNavigationItem;
@synthesize registerViewController = _registerViewController;
@synthesize userNameTextField = _userNameTextField;
@synthesize passwordTextField = _passwordTextField;

- (void)dealloc
{
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    self.activityIndicatorView.hidden = true;
    self.loginNavigationItem.hidesBackButton = true;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    self.activityIndicatorView = nil;
    self.loginNavigationItem = nil;
    self.registerViewController = nil;
    self.passwordTextField = nil;
    self.userNameTextField = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}

- (void)loginButtonClicked:(id)sender
{
    [self.view endEditing:YES];
    
    if([self.userNameTextField.text isEqualToString:@""] || [self.passwordTextField.text isEqualToString:@""])
    {
        [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_USERNAME_PASSWORD_CANT_EMPTY", nil)];
        return;
    }
    
    self.activityIndicatorView.hidden = false;
    [self.activityIndicatorView startAnimating];
    
    User* user = [[User alloc] init];
    user.name = self.userNameTextField.text;
    user.password = self.passwordTextField.text;
    
    [[ServerProxy sharedInstance] login:user completedAction:@selector(loginCompleted:) target:self];
}

- (void)loginCompletedImpl:(LoginResult*)loginResult
{
    self.activityIndicatorView.hidden = true;
    [self.activityIndicatorView stopAnimating];
    
    if([loginResult isOnline])
    {
        [self close];
    }
    else
    {
        if([loginResult isInvalid])
        {
            [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_USER_NOT_EXISTING", nil)];
        }
        else
        {
            [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_USER_IS_NOT_ACTIVATED", nil)];
        }
    }    
    
    [loginResult release];
}

- (void)loginCompleted:(LoginResult*)loginResult
{
    [loginResult retain];
    [self performSelectorOnMainThread:@selector(loginCompletedImpl:) withObject:loginResult waitUntilDone:NO];
}

- (void)forgetPasswordCompleted:(ForgetPasswordResult*)forgetPassResult
{
    [forgetPassResult retain];
    [self performSelectorOnMainThread:@selector(forgetPasswordCompletedImpl:) withObject:forgetPassResult waitUntilDone:NO]; 
}

- (void)forgetPasswordCompletedImpl:(ForgetPasswordResult*)forgetPassResult
{
    switch (forgetPassResult.errorCode) {
        case 0:
            [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_NEW_PASSWORD_SENT", nil)];
            break;
        case 1:
            [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_USER_NOT_EXISTING", nil)];
            break;
        case 2:
            [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_GET_PASSWORD_MANY_TIMES", nil)];
            break;
        default:
            break;
    }
    [forgetPassResult release];
}

- (void)registerButtonClicked:(id)sender
{
    [self.navigationController pushViewController:self.registerViewController animated:YES];
}

- (void)forgetPasswordButtonClicked:(id)sender
{
    if([self.userNameTextField.text isEqualToString:@""])
    {
        [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_USERNAME_CANT_EMPTY", nil)];
        return;
    }

    [self.view endEditing:YES];
    
    User* user = [[[User alloc] init] autorelease];
    user.name = self.userNameTextField.text;
        
    [[ServerProxy sharedInstance] forgetPassword:user completedAction:@selector(forgetPasswordCompleted:) target:self];
}

- (void)closeButtonClicked:(id)sender
{
    [self close];
}

-(void)close
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
